package com.fzz.controller;

import com.fzz.domain.Admin;
import com.fzz.domain.JsonObject;
import com.fzz.domain.User;
import com.fzz.domain.View;
import com.fzz.service.AdminService;
import com.fzz.serviceImpl.AdminServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService=new AdminServiceImpl();

    //前往管理员界面
    @RequestMapping(value = "/admin")
    public String toAdmin()
    {
        return "admin";
    }

    //清除会话数据，并退出
    @RequestMapping(value = "admin/logout")
    public String adminLogout(HttpSession session)
    {
        session.invalidate();
        return "login";
    }

    //管理员修改信息
    @RequestMapping(value = "admin/updateInfo")
    @ResponseBody
    public String adminUpdate(HttpServletRequest request,Admin admin)
    {
        /*Admin admin=new Admin();
        admin.setAdminName(request.getParameter("adminName"));
        admin.setPassword(request.getParameter("password"));
        admin.setEmail(request.getParameter("email"));
        admin.setPhone(request.getParameter("phone"));
        admin.setEmail(request.getParameter("email"));*/
        HttpSession session=request.getSession();
        Integer rows = adminService.adminUpdate(admin);
        if(rows>0)
        {
            session.setAttribute("admin",admin);
            System.out.println(admin);
            System.out.println("success");
            return "OK";
        }
        else {
            System.out.println("fail");
        }
        return "NO";
    }

    //分页查询用户信息
    @RequestMapping(value = "admin/selectUserWithPage")
    @ResponseBody
    public JsonObject selectUserWithPage(Model model,Integer id,String username,HttpServletRequest request,@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int count)
    {

        System.out.println(id);
        PageInfo<User> users=adminService.selectUserWithPage(model,id,username,page,count);
        model.addAttribute("userList",users);
        System.out.println(users);
        JsonObject jsonObject=new JsonObject();
        jsonObject.setCode(0);
        jsonObject.setCount(users.getTotal());
        jsonObject.setMsg("");
        jsonObject.setData(users.getList());
        System.out.println(jsonObject);
        return jsonObject;
    }
    //分页查询用户信息
    @RequestMapping(value = "admin/toAdmin")
    public String toAdmin(Model model, Integer id,String username,@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int count)
    {
        PageInfo<User> users=adminService.selectUserWithPage(model,id,username,page,count);
        model.addAttribute("userList",users);
        return "admin_user";
    }

    //通过id查询用户信息
    @RequestMapping(value = "/admin/selectUserById")
    public String selectUserById(HttpSession session, Integer id)
    {
        System.out.println("ID"+id);
        User user = adminService.selectUserById(id);
        System.out.println("User"+user);
        session.setAttribute("user",user);
        return "admin_user";
    }

    //修改用户信息
    @RequestMapping(value = "/admin/editUserInfo")
    @ResponseBody
    public String editUserInfo(HttpSession session,User user)
    {
        Integer rows = adminService.editUserInfo(user);
        if(rows>0)
        {
            return "OK";
        }
        System.out.println(user);
        return "NO";
    }

    //删除用户信息
    @RequestMapping("/admin/deleteUser")
    @ResponseBody
    public String deleteUser(Integer id)
    {
        System.out.println("ID"+id);
        Integer rows = adminService.deleteUserInfoById(id);
        if(rows>0)
        {
            return "OK";
        }
        return "NO";
    }
}
